package service

import (
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
)

type ClustersService struct {
}

/*
*
新增k8s集群
*/
func (ClustersService) AddCluster(ctx *gin.Context) {
	filename := ctx.PostForm("clusterName") + "_kubeconfig"
	// 指定保存路径
	savePath := "conf/k8sConf/" + filename
	if _, err := os.Stat(savePath); err == nil {
		ctx.JSON(http.StatusOK, gin.H{
			"messge": "clusterName is already exists",
		})
		return
	}
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error":   "文件上传失败",
			"details": err.Error(),
		})
		return
	}
	// 保存文件
	if err := ctx.SaveUploadedFile(file, savePath); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error":   "文件保存失败",
			"details": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"messge": "success",
	})
}

func (ClustersService) ChangeCluster(ctx *gin.Context) {
	return
}
